package org.keyyond.ms.business.util;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

public class ValidatorUtil {
    

    /**
     * 使用validator进行数据校验
     * @author YCR
     * @param
     * @return String
     * @throws
     */
    public static String validate(Object o) {
	StringBuffer sb = new StringBuffer();
	ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
	Validator validator = vf.getValidator();
	Set<ConstraintViolation<Object>> set = validator.validate(o);
	for (ConstraintViolation<Object> constraintViolation : set) {
	    System.out.println(constraintViolation.getPropertyPath() + ":" + constraintViolation.getMessage());
	    sb.append(constraintViolation.getPropertyPath() + ":" + constraintViolation.getMessage());
	}
	return sb.toString();
    }

}
